package cn.edu.hqu.cst.android.ls.media;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


public class SearchVideo extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_video);

       File directory_video = Environment.getExternalStoragePublicDirectory(Environment. DIRECTORY_MOVIES);
        //使用这个方法需要传入公共目录的类型如Environment.DIRECTORY_DOCUMENTS
        //查看公共目录文档文件的路径
        Log.e("main","视频目录:"+directory_video);

        }
    public void requestPower() {
//判断是否已经赋予权限
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            //如果应用之前请求过此权限但用户拒绝了请求，此方法将返回 true。
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.READ_EXTERNAL_STORAGE)) {//这里可以写个对话框之类的项向用户解释为什么要申请权限，并在对话框的确认键后续再次申请权限.它在用户选择"不再询问"的情况下返回false
            } else {
                //申请权限，字符串数组内是一个或多个要申请的权限，1是申请权限结果的返回参数，在onRequestPermissionsResult可以得知申请结果
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,}, 1);
            }
        }
    }
    }

 class MMM extends SearchVideo{


    String name;
    long size;
    String url;
    long duration;
    public String getName1() {
        return name;
    }
    public void setName1(String name) {
        this.name = name;
    }
    public long getSize1() {
        return size;
    }
    public void setSize1(long size) {
        this.size = size;
    }
    public String getUrl1() {
        return url;
    }
    public void setUrl1(String url) {
        this.url = url;
    }
    public long getDuration1() {
        return duration;
    }
    public void setDuration1(long duration) {
        this.duration = duration;
    }

     public List<MMM> getList() {
         List<MMM> list = null;
         if (this != null) {
             Cursor cursor = this.getContentResolver().query(
                     MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null,
                     null, null);
             if (cursor != null) {
                 list = new ArrayList<MMM>();
                 while (cursor.moveToNext()) {
                     int id = cursor.getInt(cursor
                             .getColumnIndexOrThrow(MediaStore.Video.Media._ID));
                     String title = cursor
                             .getString(cursor
                                     .getColumnIndexOrThrow(MediaStore.Video.Media.TITLE));
                     String album = cursor
                             .getString(cursor
                                     .getColumnIndexOrThrow(MediaStore.Video.Media.ALBUM));
                     String artist = cursor
                             .getString(cursor
                                     .getColumnIndexOrThrow(MediaStore.Video.Media.ARTIST));
                     String displayName = cursor
                             .getString(cursor
                                     .getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME));
                     String mimeType = cursor
                             .getString(cursor
                                     .getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE));
                     String path = cursor
                             .getString(cursor
                                     .getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
                     long duration = cursor
                             .getInt(cursor
                                     .getColumnIndexOrThrow(MediaStore.Video.Media.DURATION));
                     long size = cursor
                             .getLong(cursor
                                     .getColumnIndexOrThrow(MediaStore.Video.Media.SIZE));
                     MMM video = new MMM();
                     video.setName1(title);
                     video.setSize1(size);
                     video.setUrl1(path);
                     video.setDuration1(duration);
                     list.add(video);
                 }
                 cursor.close();
             }
         }
         return list;

     }







 }






